from selenium.webdriver.support.select import Select
from page.base_page import BasePage
from selenium.webdriver.common.by import By

class BusinessAddPage(BasePage):
    """商机页面"""
    url = '/crm5/index.php?m=business&a=add'

    """定位器"""
    # 商机名
    business_name_locator = (By.NAME, 'name')
    # 商机金额
    money_positive_number_locator = (By.ID, 'total_price')
    # 成交价
    estimate_price_locator = (By.ID, 'estimate_price')
    # 所属客户
    customer_name_locator = (By.ID, 'customer_name')
    # 选择客户
    choice_customer_locator = (By.CSS_SELECTOR,
                               '#datas > tr:nth-child(1) > td:nth-child(1) > input[type=radio]:nth-child(1)')

    # 确认
    btn_sure_locator = (By.XPATH, '/html/body/div[10]/div[3]/div/button[1]/span')
    # 保存
    btn_save_locator = (By.XPATH, '//*[@id="form1"]/table/tfoot/tr/td/input[1]')
    # 赢单率
    win_rate_locator = (By.ID, 'gain_rate')
    #商机来源
    business_origin_locator = (By.ID, 'origin')


    def input_business_name(self,business_name):
        """输入商机名"""
        self.driver.find_element(*self.business_name_locator).send_keys(business_name)

    def input_business_money(self,money):
        """输入商机金额"""
        self.driver.find_element(*self.money_positive_number_locator).send_keys(money)

    def input_estimate_price(self,estimate_price):
        """输入成交价"""
        self.driver.find_element(*self.estimate_price_locator).send_keys(estimate_price)

    def click_btn_save(self):
        """点击保存"""
        self.driver.find_element(*self.btn_save_locator).click()

    def click_customer_name(self):
        """点击所属客户"""
        self.driver.find_element(*self.customer_name_locator).click()

    def click_choice_customer(self):
        """选择客户"""
        self.driver.find_element(*self.choice_customer_locator).click()

    def btn_sure(self):
        """点击确认"""
        self.driver.find_element(*self.btn_sure_locator).click()

    def input_win_rate(self,win_rate):
        """输入赢单率"""
        self.driver.find_element(*self.win_rate_locator).send_keys(win_rate)

    def select_business_origin(self,origin_text='电话营销'):
        """点击商机来源"""
        origin_element = self.driver.find_element(*self.business_origin_locator)
        select = Select(origin_element)
        select.select_by_visible_text(origin_text)
